<cfparam name="view" default="" />
<cfparam name="action" default="" />
<cfparam name="alert" default="" />
<cfparam name="email" default="" />
<cfparam name="j_username" default="" />

<cflogout />

<cfswitch expression="#action#">
	<cfcase value="emailPassword">
		<cfscript>
			base = new assets.calgrants.com.Base();
			emailResult = base.emailPassword(Email = Email);
		</cfscript>
		<cfif emailResult>
			<cfset alert = "Your password has been sent to #email#" />
			<cfset view = "login" />
		<cfelse>
			<cfset alert = "The e-mail address #email# was not found.  Verify your e-mail address and try again." />
			<cfset view = "password" />
		</cfif>
	</cfcase>
	<cfcase value="checkEmail">
		<cfscript>
			userGateway = new assets.calgrants.com.gateway.UserGateway();
			usersWithEmail = userGateway.query(Email = Email, Deleted = 0, Archived = 0);
		</cfscript>
		<cfif usersWithEmail.RecordCount gt 1>
			<cfset alert = "An account already exists for #email#, however there is a problem with the account.  Please contact #application.personality.name# at #application.personality.email# to resolve this issue." />
			<cfset view = "login" />
		<cfelseif usersWithEmail.RecordCount eq 1>
			<cfset view = "userExists" />
		<cfelseif usersWithEmail.RecordCount eq 0>
			<cfset view = "user" />
		</cfif>
	</cfcase>
	<cfcase value="packUser">
		<cfset formClone = Duplicate(form) />
		<cfset StructDelete(formClone, "action") />
		<cfset StructDelete(formClone, "fieldnames") />
		<cfwddx action="cfml2wddx" input="#formClone#" output="userWDDX" />
		<cfset view = "organizations" />
	</cfcase>
	<cfcase value="checkOrganization">
		<cfif Organization_ID eq 0>
			<cfset view = "organization" />
		<cfelseif Organization_ID gt 0>
			<cfscript>
				userGateway = new assets.calgrants.com.gateway.UserGateway();
				organizers = userGateway.query(Organization_ID = Organization_ID, Approved = 1, Role = "Organizer");
			</cfscript>
			<cfwddx action="wddx2cfml" input="#userWDDX#" output="user" />
			<cfset user.Organization_ID = Organization_ID />
			<cfif organizers.RecordCount eq 0>
				<cfset user.Approved = 1 />
				<cfset user.Role = "Organizer" />
				<cfset alert = "Your account has been created.\n\nYou may now login." />
				<cfset view = "login" />
			<cfelseif organizers.RecordCount gt 0>
				<cfset user.Approved = 0 />
				<cfset user.Role = "Site Contact" />
				<cfset view = "createdOnExisting" />
			</cfif>
			<cfscript>
				user = userGateway.save(argumentCollection = user);
			</cfscript>
			<cfif NOT IsInstanceOf(user, "assets.calgrants.com.bo.User")>
				<cfset alert = "There was a problem creating your account.  Please try again.">
				<cfset view = "error">
			</cfif>
		</cfif>	
	</cfcase>
	<cfcase value="createAccount">
		<cfwddx action="wddx2cfml" input="#form.userWDDX#" output="userArgs" />
		<cfif form.Organization_ID eq 0>
			<cfscript>
				organizationGateway = new assets.calgrants.com.gateway.OrganizationGateway();
				organization = organizationGateway.save(argumentCollection = form);
			</cfscript>
			<!--- need to mail notification to CCFC that new organization is create and pending approval --->
			<cfif NOT IsInstanceOf(organization, "assets.calgrants.com.bo.Organization")>
				<cfset alert = "There was a problem creating your account.  Please try again.">
				<cfset view = "error">
			<cfelse>
				<cfset userArgs.Organization_ID = organization.getOrganization_ID()>
				<cfset userArgs.Approved = 1 />
				<cfset userArgs.Role = "Organizer" />
				<cfscript>
					userGateway = new assets.calgrants.com.gateway.UserGateway();
					user = userGateway.save(argumentCollection = userArgs);
				</cfscript>
				<cfif NOT IsInstanceOf(user, "assets.calgrants.com.bo.User")>
					<cfset alert = "There was a problem creating your account.  Please try again.">
					<cfset view = "error">
				<cfelse>
					<cfset alert = "Your account has been created.\n\nYou may now login." />
					<cfset view = "login" />
				</cfif>
			</cfif>
		<cfelse>
			<cfset userArgs.Approved = 0 />
			<cfset userArgs.Role = "Site Contact" />
			<cfscript>
				userGateway = new assets.calgrants.com.gateway.UserGateway();
				user = userGateway.save(argumentCollection = userArgs);
			</cfscript>
			<cfif NOT IsInstanceOf(user, "assets.calgrants.com.bo.User")>
				<cfset alert = "There was a problem creating your account.  Please try again.">
				<cfset view = "error">
			<cfelse>
				<cfset view = "createdOnExisting" />
			</cfif>
		</cfif>
		<!--- <cfset organizationArgs = Duplicate(form) />
		<!--- <cfset StructDelete(organization, "action") />
		<cfset StructDelete(organization, "fieldnames") /> --->
		<cfset organizationArgs.approved = 0 />
		<cfset organizationArgs.deleted = 0 />
		<cfset organizationArgs.archived = 0 />
		<cfscript>
			organizationGateway = new assets.calgrants.com.gateway.OrganizationGateway();
			organization = organizationGateway.save(argumentCollection = organizationArgs);
		</cfscript>
		<!--- <cfinvoke component="#application.pathToGateways#.OrganizationGateway" method="save" returnvariable="organization">
			<cfinvokeargument name="argumentCollection" value="#organization#">
		</cfinvoke> --->
		<cfwddx action="wddx2cfml" input="#userWDDX#" output="userArgs" />
		<cfset userArgs.organization_id = organization.getOrganization_ID() />
		<cfset userArgs.role = "Organizer" />
		<cfset userArgs.approved = 1 />
		<cfset userArgs.deleted = 0 />
		<cfset userArgs.archived = 0 />
		<cfscript>
			userGateway = new assets.calgrants.com.gateway.UserGateway();
			user = userGateway.save(argumentCollection = userArgs);
		</cfscript>
		<!--- <cfinvoke component="#application.pathToGateways#.UserGateway" method="save" returnvariable="user">
			<cfinvokeargument name="argumentCollection" value="#user#">
		</cfinvoke> --->
		<cfif IsNumeric(user.getUser_ID()) and user.getUser_ID() gt 0>
			<cfset alert "Your account has been created.\n\nYou may now login." />
			<cfset view = "login" />
		<cfelse>
			<cfset view = "error" />
		</cfif> --->
	</cfcase>
</cfswitch>

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Cash for College Workshop Management System</title>
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta name="description" content="Cash for College Workshop Management System">
	<meta name="author" content="Syntric">

	<cfoutput>
		<link href="#request.pathToBootstrapCSS#" rel="stylesheet" />
		<link href="#request.pathToKendoCommonCSS#" rel="stylesheet" />
		<link href="#request.pathToKendoDefaultCSS#" rel="stylesheet" />
		<link href="#request.pathToFontAwesomeCSS#" rel="stylesheet" />
		<link href="#request.pathToUI#/common.css" rel="stylesheet" />
		<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800" rel="stylesheet" type="text/css" />
		<script src="#request.pathToJQuery#" type="text/javascript"></script>
		<script src="#request.pathToBootstrap#" type="text/javascript"></script>
		<script src="#request.pathToKendo#" type="text/javascript"></script>
		<script src="#request.pathToUI#/common.js" type="text/javascript"></script>
		<script type="text/javascript">
			<cfif Len(alert) gt 0>
				alert("#alert#");
			</cfif>
			<cfif ArrayLen(request.alerts) gt 0>
				<cfloop index="i" from="1" to="#ArrayLen(request.alerts)#">
					alert("#request.alerts[i]#");
				</cfloop>
			</cfif>
			var loginReg = function() {
				var email = $("##j_username").val();
				if (email == "despinacosto@gmail.com" || email == "despina@ccfc.org" || email == "ken@syntric.com" || email == "swilliams@msjc.edu") {
					var f = $("##loginForm");
					f.submit();
				} else {
					alert("Registration for the 2013-2014 Cash for College Program is not open yet.\n\nOrganizers from prior program year will be notified by email when the registration opens.");
				}
				return true;				
			}
		</script>
	</cfoutput>

</head>

<body style="background-color: white;">

<cfoutput>
	<div class="container-fluid" style="border: solid 1px red;">
		<div class="row-fluid" style="height: 20px;">
			<div class="span12"></div>
		</div>
		<cfswitch expression="#view#">
			<cfcase value="password">
				<div class="row-fluid">
					<div class="span3"></div>
					<div class="span6">
						<img src="common/images/Cash_for_College_logo.gif" width="204" height="54" style="margin-bottom: 8px;">
						<p><strong>Workshop Management System for Organizers &amp; Site Contacts</strong></p>
						<div class="well well-large well-framed" style="width: 400px; margin-bottom: 40px;">
							<cfform id="passwordForm" action="?action=emailPassword" method="post">
								<div class="clearfix">
									<label for="email">Enter your e-mail address</label>
									<div class="input-prepend">
										<span class="add-on">
											<i class="icon-envelope"></i>
										</span>
										<cfinput type="text" name="email" value="#email#"  validate="email" required="yes" message="E-mail is required and must be a valid e-mail address" style="width: 90%;" />
									</div>
								</div>
								<div class="btn-toolbar clearfix">
									<button type="submit" name="sendPasswordButton" class="btn btn-success">Send Password</button>
									<button type="button" name="cancelButton" class="btn btn-success" onClick="document.location.href='?view=login'">Cancel</button>
								</div>
							</cfform>
						</div>
					</div>
					<div class="span3"></div>
				</div>
			</cfcase>
			<cfcase value="email">
				<div class="row-fluid">
					<div class="span3"></div>
					<div class="span6">
						<img src="common/images/Cash_for_College_logo.gif" width="204" height="54" style="margin-bottom: 8px;">
						<p><strong>Workshop Management System for Organizers &amp; Site Contacts</strong></p>
						<div class="well well-large well-framed" style="width: 400px; margin-bottom: 40px;">
							<cfform id="emailForm" action="#script_name#" method="post">
								<cfinput type="hidden" name="action" value="checkEmail">
								<div class="clearfix">
									<label for="name">Enter your e-mail address</label>
									<div class="input-prepend">
										<span class="add-on">
											<i class="icon-envelope"></i>
										</span>
										<cfinput type="text" name="email" value="#email#" required="yes" message="E-mail is required and must be a valid e-mail address" style="width: 90%;" />
									</div>
								</div>
								<div class="btn-toolbar clearfix">
									<button type="submit" class="btn btn-success">Next</button>
									<button type="button" name="cancelButton" class="btn btn-success" onClick="document.location.href='?view=login'">Cancel</button>
								</div>
							</cfform>
						</div>
					</div>
					<div class="span3"></div>
				</div>
			</cfcase>
			<cfcase value="userExists">
				<div class="row-fluid">
					<div class="span3"></div>
					<div class="span6">
						<img src="common/images/Cash_for_College_logo.gif" width="204" height="54" style="margin-bottom: 8px;">
						<p><strong>Workshop Management System for Organizers &amp; Site Contacts</strong></p>
						<div class="well well-large well-framed" style="width: 400px; margin-bottom: 40px;">
							<div class="clearfix">
								An account already exists for #email#.
							</div>
							<div class="btn-toolbar clearfix">
								<button type="button" name="sendPasswordButton" class="btn btn-success" onClick="document.location.href='?action=emailPassword&Email=#email#'">Send Password</button>
								<button type="button" name="cancelButton" class="btn btn-success" onClick="document.location.href='?view=login'">Cancel</button>
							</div>
						</div>
					</div>
					<div class="span3"></div>
				</div>
			</cfcase>
			<cfcase value="user">
				<div class="row-fluid">
					<div class="span3"></div>
					<div class="span6">
						<img src="common/images/Cash_for_College_logo.gif" width="204" height="54" style="margin-bottom: 8px;">
						<p><strong>Workshop Management System for Organizers &amp; Site Contacts</strong></p>
						<div class="well well-large well-framed" style="width: 400px; margin-bottom: 40px;">
							<cfform id="userForm" action="#script_name#" method="post">
								<cfinput type="hidden" name="action" value="packUser" />
								<cfinput type="hidden" name="user_id" value="0" />
								<cfinput type="hidden" name="email" value="#email#" />
								<div class="clearfix">
									<label for="name">First Name</label>
									<cfinput type="text" name="first_name" value="" required="yes" message="First Name is required" class="span12" />
								</div>
								<div class="clearfix">
									<label for="name">Last Name</label>
									<cfinput type="text" name="last_name" value="" required="yes" message="Last Name is required" class="span12" />
								</div>
								<div class="clearfix">
									<label for="title">Title</label>
									<cfinput type="text" name="title" value="" class="span12" />
								</div>
								<div class="text-control clearfix">
									<label for="email">E-mail</label>
									#email#
								</div>
								<div class="clearfix">
									<label for="password">Password</label>
									<cfinput type="password" name="password" value="" required="yes" message="Password is required" class="span12" />
								</div>
								<div class="row-fluid clearfix">
									<div class="span9">
										<label for="phone">Phone</label>
										<cfinput type="text" name="phone" value="" required="yes" validate="telephone" message="Phone is required and must be in a valid format" class="span12" />
									</div>
									<div class="span3">
										<label for="phone">Extension</label>
										<cfinput type="text" name="Extension" value="" required="no" maxlength="10" class="span12" />
									</div>
								</div>
								<div class="btn-toolbar clearfix">
									<button type="submit" class="btn btn-success">Next</button>
									<button type="button" name="cancelButton" class="btn btn-success" onClick="document.location.href='?view=login'">Cancel</button>
								</div>
							</cfform>
						</div>
					</div>
					<div class="span3"></div>
				</div>
			</cfcase>
			<cfcase value="organizations">
				<cfscript>
					organizationGateway = new assets.calgrants.com.gateway.OrganizationGateway();
					organizations = organizationGateway.query(Approved = 1, Archived = 0);
				</cfscript>
				<div class="row-fluid">
					<div class="span3"></div>
					<div class="span6">
						<img src="common/images/Cash_for_College_logo.gif" width="204" height="54" style="margin-bottom: 8px;">
						<p><strong>Workshop Management System for Organizers &amp; Site Contacts</strong></p>
						<div class="well well-large well-framed" style="width: 600px; margin-bottom: 40px;">
							<cfform id="organizationsForm" action="#script_name#" method="post">
								<cfinput type="hidden" name="action" value="checkOrganization">
								<cfinput type="hidden" name="userWDDX" value="#userWDDX#" />
								<div class="clearfix">
									<label for="name">Select your organization</label>
									<p>If your organization is not listed, select &quot;My organization is not listed&quot; at the bottom of the list.</p>
									<cfselect name="organization_id" size="20" required="yes" message="Organization is required.\n\nIf your organization is not listed select \'Not listed.\'" style="width: 100%;">
										<cfloop query="organizations">
											<option value="#organization_id#">#organization#, #city#</option>
										</cfloop>
										<option value="0">My organization is not listed</option>
									</cfselect>
								</div>
								<div class="btn-toolbar clearfix">
									<button type="submit" class="btn btn-success">Next</button>
									<button type="button" name="cancelButton" class="btn btn-success" onClick="document.location.href='?view=login'">Cancel</button>
								</div>
							</cfform>
						</div>
					</div>
					<div class="span3"></div>
				</div>
			</cfcase>
			<cfcase value="organization">
				<cfinvoke component="#application.pathToGateways#.CountyGateway" method="query" returnvariable="counties" />
				<div class="row-fluid">
					<div class="span3"></div>
					<div class="span6">
						<img src="common/images/Cash_for_College_logo.gif" width="204" height="54" style="margin-bottom: 8px;">
						<p><strong>Workshop Management System for Organizers &amp; Site Contacts</strong></p>
						<div class="well well-large well-framed" style="width: 400px; margin-bottom: 40px;">
							<cfform id="organizationForm" action="#script_name#" method="post">
								<cfinput type="hidden" name="action" value="createAccount">
								<cfinput type="hidden" name="userWDDX" value="#userWDDX#">
								<cfinput type="hidden" name="organization_id" value="#organization_id#" />
								<div class="clearfix">
									<label for="name">Organization</label>
									<cfinput type="text" name="organization" value="" required="yes" message="Organization is required" class="span9" />
								</div>
								<div class="clearfix">
									<label for="name">Address</label>
									<cfinput type="text" name="address" value="" required="yes" message="Address is required" class="span9" />
								</div>
								<div class="clearfix">
									<label for="name">City</label>
									<cfinput type="text" name="city" value="" required="yes" message="City is required" class="span9" />
								</div>
								<div class="clearfix">
									<label for="name">Zip Code</label>
									<cfinput type="text" name="zip_code" value="" required="yes" message="Zip Code is required" />
								</div>
								<div class="clearfix">
									<label for="name">County</label>
									<cfselect name="county_id" required="yes" message="County is required" class="span9">
										<option value="">Select...</option>
										<cfloop query="counties">
											<option value="#county_id#">#county#</option>
										</cfloop>
									</cfselect>
								</div>
								<div class="btn-toolbar clearfix">
									<button type="submit" class="btn btn-success">Create Account</button>
									<button type="button" name="cancelButton" class="btn btn-success" onClick="document.location.href='?view=login'">Cancel</button>
								</div>
							</cfform>
						</div>
					</div>
					<div class="span3"></div>
				</div>
			</cfcase>
			<cfcase value="createdOnExisting">
				<cfinvoke component="#application.pathToGateways#.OrganizationGateway" method="query" returnvariable="qOrganization">
					<cfinvokeargument name="Organization_ID" value="#Organization_ID#" />
				</cfinvoke>
				<cfinvoke component="#application.pathToGateways#.UserGateway" method="query" returnvariable="qRegionalAdmins">
					<cfinvokeargument name="Approved" value="1" />
					<cfinvokeargument name="Deleted" value="0" />
					<cfinvokeargument name="Archived" value="0" />
					<cfinvokeargument name="Role" value="Regional Admin" />
					<cfinvokeargument name="Region_IDs" value="#qOrganization.Region_ID#" />
				</cfinvoke>
				<div class="row-fluid">
					<div class="span3"></div>
					<div class="span6">
						<img src="common/images/Cash_for_College_logo.gif" width="204" height="54" style="margin-bottom: 8px;">
						<p><strong>Workshop Management System for Organizers &amp; Site Contacts</strong></p>
						<div class="well well-large well-framed" style="width: 400px; margin-bottom: 40px;">
							<p>Your account has been created.  However it must be approved by an organizer before you can login.</p>

							<p>Organizers for #qOrganization.Organization# are:</p>
							
							<ul>
								<cfloop query="organizers">
									<li>#first_name# #last_name# - #email# - #phone#<cfif Len(Trim(extension)) gt 0> x #extension#</cfif></li>
								</cfloop>
							</ul>
							
							<p>Each organizer listed above has been sent an e-mail message about your account and informs them that your account must be approved before you can login.</p>
							
							<p>If none of the organizers listed above are available to approve your account you can contact:</p>
							
							<ul>
								<cfloop query="qRegionalAdmins">
									<li>#first_name# #last_name# - #email# - #phone#<cfif Len(Trim(extension)) gt 0> x #extension#</cfif> (Regional Administrator)</li>
								</cfloop>
								<cfif qRegionalAdmins.RecordCount eq 0>
									<li>#request.personality.name# - #request.personality.email# - #request.personality.phone# (Statewide Coordinator)</li>
									<li>#request.developer.name# - #request.developer.email# - #request.developer.phone# (Developer)</li>
								</cfif>
							</ul>
							
							<p><a href="?view=login" class="btn btn-success">Go to Login</a></p>
						</div>
					</div>
					<div class="span3"></div>
				</div>
			</cfcase>
			<cfcase value="error">
				<div class="row-fluid">
					<div class="span3"></div>
					<div class="span6">
						<img src="common/images/Cash_for_College_logo.gif" width="204" height="54" style="margin-bottom: 8px;">
						<p><strong>Workshop Management System for Organizers &amp; Site Contacts</strong></p>
						<div class="well well-large well-framed" style="width: 400px; margin-bottom: 40px;">
							<p>There was a problem with your submission.  Please start over.</p>
						</div>
					</div>
					<div class="span3"></div>
				</div>
			</cfcase>
			<cfdefaultcase>
				<div class="row-fluid">
					<div class="span3"></div>
					<div class="span6">
						<img src="common/images/Cash_for_College_logo.gif" width="204" height="54" style="margin-bottom: 0">
						<h5><strong>Workshop Management System for Organizers &amp; Site Contacts</strong></h5>
						<div class="well well-large well-framed" style="width: 400px; margin-bottom: 40px;">
							<cfform id="loginForm" action="index.cfm" method="post">
								<div class="clearfix">
									<label for="name">E-mail</label>
									<div class="input-prepend">
										<span class="add-on">
											<i class="icon-envelope"></i>
										</span>
										<cfinput type="text" name="j_username" value="#j_username#" required="yes" validate="email" message="E-mail is required and must be a valid e-mail address" style="width: 90%;" />
									</div>
								</div>
								<div class="clearfix">
									<label for="password">Password</label>
									<div class="input-prepend">
										<span class="add-on">
											<i class="icon-lock"></i>
										</span>
										<cfinput type="password" name="j_password" required="yes" message="Password is required" style="width: 90%;" />
									</div>
								</div>
								<div class="btn-toolbar clearfix">
									<!--- <button type="button" class="btn btn-success" onClick="loginReg();">Login</button> --->
									<button type="submit" name="loginButton" class="btn btn-success">Login</button>
								</div>
							</cfform>
							<div style="margin-top: 10px;">
								<a href="?view=email">Create an account</a><br>
								<a href="?view=password">Forgot your password?</a>
							</div>
						</div>
					</div>
					<div class="span3"></div>
				</div>
			</cfdefaultcase>
		
		</cfswitch>
		<div class="row-fluid body-row">
			<div class="span3"></div>
			<div class="span6">
				<div style="margin-top: 20px;">
				<small>
				<p><strong>Cash for College</strong> is California's public-private partnership FAFSA completion program administered by the California Student Aid Commission and its regional and local partners.</p>
				<p><strong>Our mission</strong> is to assist high school seniors and recent graduates who are low income or first generation students in accessing as much financial aid as possible. California Cash for College puts California Dreams into Action by helping students complete the Free Application for Federal Student Aid (FAFSA) or California Dream Act Application. Students who apply for financial aid take action to enroll into education beyond high school.</p>
				</small>
				</div>
			</div>
			<div class="span3"></div>
		</div>
	</div>
</cfoutput>

</body>
</html>